﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Projektarbete
{
    class TileData
    {
        public LinkedList<Item> linkedListItem;
        public Item item;

        public TileData()
        {

        }

        public TileData(LinkedList<Item> itemContent)
        {
            linkedListItem = itemContent;
        }

        public TileData(Item item)
        {
            this.item = item;
        }
    }

    class Tile
    {
        //TODO: Be able to change textures?
        public ushort textureID;

        public ushort texture { get { return textureID; } set { textureID = value; } }

        /// <summary>
        /// Creates a tile class
        /// </summary>
        /// <param name="textureID">Which texture to draw</param>
        /// <param name="propertyID">Which collision data this tile should use</param>
        public Tile(ushort textureID)
        {
            this.textureID = textureID;
        }

        /// <summary>
        /// Creates a tile class
        /// </summary>
        /// <param name="textureID">Which texture to draw</param>
        /// <param name="propertyID">Which collision data this tile should use</param>
        /// <param name="interactedState">Which state of interaction this tile should start in</param>
        public Tile(ushort textureID, byte interactedState)
        {
            this.textureID = textureID;
        }

        /// <summary>
        /// Gets data from this tile (if there is any)
        /// USE WITH CARE
        /// </summary>
        public virtual TileData GetData()
        {
            return new TileData();
        }

        /// <summary>
        /// Sets this tile's data (if there is any)
        /// </summary>
        public virtual void SetData(TileData data)
        {

        }

        /// <summary>
        /// Interact with the selected tile
        /// </summary>
        public virtual void Interact()
        {

        }
    }

    class Door : Tile
    {
        public byte interactedState;

        public Door(ushort textureID)
            : base(textureID)
        {
            interactedState = 0;
        }

        public Door(ushort textureID, byte interactedState)
            : base(textureID, interactedState)
        {
            this.interactedState = interactedState;
        }

        public override void Interact()
        {
            if (interactedState == 0)
            {
                interactedState = 1;
                textureID = 3;
            }
            else
            {
                interactedState = 0;
                textureID = 2;
            }
        }
    }

    class Chest : Tile
    {
        LinkedList<Item> contents;

        VoidPointer OpenChestPopup;

        public Chest(VoidPointer OpenChestPopup)
            : base(4)
        {
            contents = new LinkedList<Item>();

            this.OpenChestPopup = OpenChestPopup;
        }

        public override TileData GetData()
        {
            return new TileData(contents);
        }

        public override void SetData(TileData data)
        {
            contents.AddLast(data.item);
        }

        public override void Interact()
        {
            OpenChestPopup();
        }
    }

    class Merchant
    {
        protected MerchantInventory inventory;
        protected SpriteFont spriteFont;
        protected Texture2D spriteSheet;

        protected Vector2 position;
        public Vector2 Position
        {
            get { return position; }
        }

        protected ushort gold;
        public ushort Gold
        {
            get { return gold; }
        }

        protected float disposition;
        public float Disposition
        {
            get { return disposition; }
        }

        protected string name;
        public string Name
        {
            get { return name; }
        }

        protected string[] dialogue;

        protected byte state;
        protected byte dialogueState;

        public Point GetTiledPosition()
        {
            return new Point((int)Math.Floor(position.X / 24), (int)Math.Floor(position.Y / 24));
        }

        public void Interact(ref HUD currentHUD)
        {
            //currentHUD = HUD;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(spriteSheet, position, new Rectangle(state * 24, 0, 24, 24), Color.White);
        }

        public void drawInventory(SpriteBatch spriteBatch)
        {
            for (int y = 0; y < inventory.Count; y++)
            {
                spriteBatch.Draw(inventory.contents.ElementAt(y).getTexture, new Vector2(800, y * 24), Color.White);
            }
        }
    }

    class PotionMerchant : Merchant
    {
        public PotionMerchant(string name, string[] dialogue, ushort startingGold, sbyte disposition, Vector2 position, Texture2D spriteSheet, SpriteFont spriteFont, HUD HUD)
        {
            this.name = name;
            this.dialogue = dialogue;
            this.gold = startingGold;
            this.disposition = disposition / 100;
            this.position = position;
            this.spriteSheet = spriteSheet;
            this.spriteFont = spriteFont;
            state = 0;
            dialogueState = 0;
        }

        public PotionMerchant(string name, string[] dialogue, ushort startingGold, sbyte disposition, Vector2 position, Texture2D spriteSheet, SpriteFont spriteFont, HUD HUD, MerchantInventory inventory)
        {
            this.name = name;
            this.dialogue = dialogue;
            this.gold = startingGold;
            this.disposition = disposition / 100;
            this.position = position;
            this.spriteSheet = spriteSheet;
            this.spriteFont = spriteFont;
            this.inventory = inventory;
            state = 0;
            dialogueState = 0;
        }
    }
}
